package logic

import (
	"context"
	"gorm.io/gorm"
	"merchantmanager/merchantrpc/mysql"

	"merchantmanager/merchantrpc/internal/svc"
	"merchantmanager/merchantrpc/merchantrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLogic {
	return &UpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 修改
func (l *UpdateLogic) Update(in *merchantrpc.UpdateRequest) (*merchantrpc.UpdateResponse, error) {
	// todo: add your logic here and delete this line
	merchant := mysql.Merchant{
		Model:   gorm.Model{ID: uint(in.Id)},
		Name:    in.Name,
		Sex:     in.Sex,
		Age:     in.Age,
		Address: in.Address,
	}
	mysql.DB.Where("id=?", in.Id).Updates(&merchant)
	return &merchantrpc.UpdateResponse{
		Id: int64(merchant.ID),
	}, nil
}
